package io.bba.boot.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.bba.boot.framework.exception.BusinessException;
import io.bba.boot.framework.page.OrderByItem;
import io.bba.boot.framework.page.OrderMapping;
import io.bba.boot.framework.page.Paging;
import io.bba.boot.app.dto.CrawlerUrlDto;
import io.bba.boot.app.entity.CrawlerUrl;
import io.bba.boot.app.mapper.CrawlerUrlMapper;
import io.bba.boot.app.query.CrawlerUrlQuery;
import io.bba.boot.app.service.CrawlerUrlService;
import io.bba.boot.app.vo.CrawlerUrlVo;
import io.bba.boot.app.query.AppCrawlerUrlQuery;
import io.bba.boot.app.vo.AppCrawlerUrlVo;
import io.bba.boot.util.IdUtil;
import io.bba.boot.util.PagingUtil;
import io.bba.boot.util.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.K;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 采集记录 服务实现类
 *
 * @author zs
 * @since 2024-03-19
 */
@Slf4j
@Service
public class CrawlerUrlServiceImpl extends ServiceImpl<CrawlerUrlMapper, CrawlerUrl> implements CrawlerUrlService {

    @Autowired
    private CrawlerUrlMapper crawlerUrlMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCrawlerUrl(CrawlerUrlDto dto) {
        CrawlerUrl crawlerUrl = new CrawlerUrl();
        BeanUtils.copyProperties(dto, crawlerUrl);
        return save(crawlerUrl);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateCrawlerUrl(CrawlerUrlDto dto) {
        Long id = dto.getId();
        CrawlerUrl crawlerUrl = getById(id);
        if (crawlerUrl == null) {
            throw new BusinessException("采集记录不存在");
        }
        BeanUtils.copyProperties(dto, crawlerUrl);
        return updateById(crawlerUrl);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCrawlerUrl(Long id) {
        return removeById(id);
    }

    @Override
    public CrawlerUrlVo getCrawlerUrlById(Long id) {
        return crawlerUrlMapper.getCrawlerUrlById(id);
    }

    @Override
    public Paging<CrawlerUrlVo> getCrawlerUrlPage(CrawlerUrlQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("create_time"));
        List<CrawlerUrlVo> list = crawlerUrlMapper.getCrawlerUrlPage(query);
        Paging<CrawlerUrlVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppCrawlerUrlVo getAppCrawlerUrlById(Long id) {
        return crawlerUrlMapper.getAppCrawlerUrlById(id);
    }

    @Override
    public Paging<AppCrawlerUrlVo> getAppCrawlerUrlPage(AppCrawlerUrlQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("create_time"));
        List<AppCrawlerUrlVo> list = crawlerUrlMapper.getAppCrawlerUrlPage(query);
        Paging<AppCrawlerUrlVo> paging = new Paging<>(list);
        return paging;
    }
}
